package app.auto.designer.func;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import app.auto.designer.R;
import app.auto.designer.ReturnActivity;
import app.auto.runner.base.ListenerBox;
import app.auto.runner.base.MapAdapter;
import app.auto.runner.base.MapViewConf;
import app.auto.runner.base.action.BaseTask;
import app.auto.runner.base.intf.MapBuilder;

public class FuncTypeActivity extends ReturnActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_func);


        Map m = (Map) getIntent().getSerializableExtra("data");
        String desc = (String) m.get("desc");
        String type = (String) m.get("type");
        String[] types = type.split(";");
        getTextView(R.id.title).setText(desc);
        List<Map> listp = new ArrayList<Map>();
        MapViewConf listmvf = MapViewConf.with(getBaseContext()).source(R.layout.auto_param_layout).pair("pdesc->name");
        MapViewConf rootmvf = MapViewConf.with(getBaseContext()).source(new HashMap(), getView(R.id.basep)).conf(listmvf).toView();

        List<Map> listptype = new ArrayList<Map>();
        for (String s : types) {
            s = s.trim();
            String[] p = s.split(" ");
            String pdesc = p[0];
            if (p.length == 1 && p[0].equals("")) {
                break;
            }
            String p2 = p[1];
            String[] p3a = p2.split(":");
            String p3name = p3a[0];
            if (p3a.length > 1) {
                String p3type = p3a[1];
            } else {
                Map pmap = MapBuilder.build().add("paramname", p3name).add("pdesc", pdesc).get();
                listp.add(pmap);
                rootmvf.source(listp, getView(R.id.basep)).toView();

            }
        }

        if (((ListView) getView(R.id.basep)).getAdapter() == null) {
            ((ListView) getView(R.id.basep)).setAdapter(new MapAdapter(this));
        }
        ((MapAdapter) ((ListView) getView(R.id.basep)).getAdapter()).addActionListener(new MapAdapter.ActionListener(R.id.value, MapAdapter.ActionListener.OnTextChanged) {


            @Override
            public void handle(MapAdapter mapAdapter, View view, int pos, ListenerBox listenerBox, Object retainedObj) {
                if (!TextUtils.isEmpty((CharSequence) retainedObj)) {
                    ((Map) mapAdapter.getItem(pos)).put("value", retainedObj.toString());
                    orderedParams.put(pos + "", retainedObj.toString());
                    result = genResult(false);
                }
            }
        });


    }

    String result = "";

    public String genResult(boolean toast) {
        String rt = "";
        for (int i = 0; i < ((ListView) getView(R.id.basep)).getAdapter().getCount(); i++) {
            if (orderedParams.containsKey("" + i)) {
                Object o = orderedParams.get("" + i);
                rt += "," + (TextUtils.isEmpty((CharSequence) o) ? "" : o.toString());
            } else {
                rt += "," + "null";
            }
        }
        rt = rt.substring(1);
        if (rt.contains("null")) {
            if (toast) {
                showToast("还有参数没有填");
                return null;
            }
        }
        return rt;
    }

    @Override
    public void finish() {
        Object result = genResult(false);
        if (result == null) {
            return;
        } else {
            setResult(1, new Intent().putExtra("status", "ok").putExtra("data", result.toString()));
        }
        super.finish();
    }

    Map<String, String> orderedParams = new HashMap<String, String>();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

}
